package com.ciquan.server.controller;

import com.ciquan.server.bean.Message;
import com.ciquan.server.bean.UserInfo;
import com.ciquan.server.bean.WorksInfo;
import com.ciquan.server.service.MessageService;
import com.ciquan.server.service.UserInfoService;
import com.ciquan.server.service.WorksInfoService;
import com.ciquan.server.util.web.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by kakalee on 15/6/7.
 */
@Controller
@RequestMapping("/api/message/")
public class ServiceMessageController extends AbstractController {

    @Resource
    private MessageService messageService;

    @Resource
    private WorksInfoService worksInfoService;

    @Resource
    private UserInfoService userInfoService;

    @RequestMapping(value = "/works", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String works(HttpServletRequest request) {
        Result result = new Result();
        int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);

        if (!isLogin(request)) {
            result.setSuccess(false);
            result.setStatusmsg("用户未登录");
            return result.toString();
        }

        List<Message> messages = messageService.selectUserMessage(userId);
        Map<Integer, Message> messageMap =  new HashMap<Integer, Message>();
        for(Message message : messages) {
            if(message.getIsRead() == 1) {
                message.setIsRead(0);
            }

            else if(message.getIsRead() == 0) {
                message.setIsRead(1);
            }

            if(message.getSendUserId() == userId) {
                message.setIsRead(0);
            }

            Message m = messageMap.get(message.getWorksId());
            if(m == null) {
                messageMap.put(message.getWorksId(), message);
            }

            else {
                m.setIsRead(m.getIsRead() + message.getIsRead());
                m.setcTime(message.getcTime());
            }
        }

        messages = new ArrayList<Message>();
        messages.addAll(messageMap.values());
        Collections.sort(messages);

        for (Message message : messages) {
            WorksInfo worksInfo = worksInfoService.selectById(message.getWorksId());
            message.setName(worksInfo.getName());
            message.setThumbPicUrl(worksInfo.getThumbPicUrl());
        }

        result.setSuccess(true);
        result.setData(messages);
        result.setStatusmsg("获取作品列表成功");
        return result.toString();
    }


    @RequestMapping(value = "/users", produces = "text/plain;charset=UTF-8", method = {RequestMethod.POST})
    @ResponseBody
    public String users(HttpServletRequest request) {
        Result result = new Result();
        int worksId = ServletRequestUtils.getIntParameter(request, "worksId", 0);
        int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);

        if (!isLogin(request)) {
            result.setSuccess(false);
            result.setStatusmsg("用户未登录");
            return result.toString();
        }

        List<Message> messages = messageService.selectReceiveUsers(worksId, userId);
        Map<Integer, Message> messageMap = new LinkedHashMap<Integer, Message>();

        for(Message message : messages) {
            if(message.getIsRead() == 1) {
                message.setIsRead(0);
            }

            else if(message.getIsRead() == 0) {
                message.setIsRead(1);
            }

            if(message.getSendUserId() == userId) {
                message.setIsRead(0);
            }


            int id = message.getSendUserId();
            if(id == userId) {
                id = message.getReceiveUserId();
            }

            Message m = messageMap.get(id);
            if(m == null) {
                messageMap.put(id, message);
                message.setSendUserId(id);
                UserInfo userInfo = userInfoService.selectById(id);
                message.setName(userInfo.getName());
                message.setThumbPicUrl(userInfo.getFace());
            }

            else {
                m.setIsRead(m.getIsRead() + message.getIsRead());
            }
        }

        result.setSuccess(true);
        result.setStatusmsg("获取用户列表成功");
        result.setData(messageMap.values());

        return result.toString();
    }
}
